package com.briup.smartcity_group6.web.controller;


import com.briup.smartcity_group6.bean.basic.Carousel;
import com.briup.smartcity_group6.service.ICarouselService;
import com.briup.smartcity_group6.utils.Result;
import com.briup.smartcity_group6.utils.ResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;

/**
 * 轮播图管理模块
 * web层：一定根据前端分离设计接口进行方法映射
 */
@Slf4j
@RequestMapping("/auth/carousel")
@Api(tags = "轮播图模块")
@RestController
public class CarouselController {
    @Autowired
    private ICarouselService service;

    @ApiOperation("查询启用的轮播图")
    @GetMapping("/yes")
    public Result findByYes(){
        log.info("web:"+Thread.currentThread().getName());
        List<Carousel> list = service.findYesCarousel();
        return ResultUtil.success(list);
    }

    @ApiOperation("新增轮播图")
    @PutMapping()
    public Result addCarousel(String carouselName,String carouselInfo,String carouselUrl){
        //1.接收参数信息，封装成一个轮播图对象(springMVC ,手动)

        //1.构造器赋值 new Carousel(1,2,3) 2. set方法:carousel.setCarouselInfo
        //3.通过建造者模式构建对象
        //Carousel carousel=new Carousel();
        //carousel.setCarouselInfo(carouselInfo);
        //carousel.setCarouselName(carouselName);
        //carousel.setCarouselUrl(carouselUrl);
        Carousel carousel = Carousel.builder().carouselName(carouselName)
                .carouselInfo(carouselInfo)
                .carouselUrl(carouselUrl).build();
        service.saveCarousel(carousel);
        return ResultUtil.success("轮播图新增成功");

    }
    @ApiOperation("更新轮播图信息")
    @PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result update(Carousel carousel){
        service.updateCarousel(carousel);
        return ResultUtil.success("轮播图更新成功");
    }

    @ApiOperation("分页查询所有轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "起始页"),
            @ApiImplicitParam(name = "pageSize",value = "页大小")
    })
    @GetMapping("/page")
    public Result findByPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize){
        PageInfo<Carousel> pageInfo = service.findAll(pageNum, pageSize);
        return ResultUtil.success(pageInfo);
    }
    //补充： 删除轮播图
    @ApiOperation("通过id删除轮播图")
    @ApiImplicitParam(name = "id",value = "轮播图id",required = true)
    @DeleteMapping
    public Result deleteById(Integer id){
        service.deleteById(id);
        return ResultUtil.success("删除成功");
    }
}
